<?php
namespace app\api\controller\service;

use app\common\controller\Api;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Comment extends Api
{
    protected $noNeedLogin = ['getList','totalComment','commentLabel','getInfo'];
    protected $noNeedRight = ['*'];


    /**
     * 查询评论列表
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList()
    {
        //$uid = $this->auth->isLogin()?$this->auth->id:'';
        $get = input('get.','','trim,strip_tags');
        $get['limit'] = 10;
        $get['page'] = input('page/d',1);
        $list = \app\api\model\service\Comment::searchComment($get);
        $this->success('信息列表返回成功',$list);
    }

    public function commentLabel()
    {
        $list = \app\api\model\service\CommentLabel::where(['state'=>1])->field('id,name')->select();
        $this->success('信息返回成功',$list);
    }



    /**
     * 统计评论数据
     * @return void
     */
    public function totalComment()
    {
        $get = input('get.','','trim,strip_tags');
        $data = '';
        try{
            $data = \app\api\model\service\Comment::totalComment($get);
        } catch (Exception $e) {
            $this->error('获取统计信息失败',$e->getMessage());
        }
        $this->success('信息列表返回成功',$data);
    }

    public function commentAdd()
    {
        $post = input('post.','','trim,strip_tags,htmlspecialchars,xss_clean');
        $post['user_id'] = $this->auth->id;
        Db::startTrans();
        try{
            \app\api\model\service\Comment::commentAdd($post);
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('订单已评论');
    }

    public function getInfo()
    {
        $id = input('order_id','');
        $comment = \app\api\model\service\Comment::where('order_id',$id)->field('score,comment_label_ids,comment_label,content,images')->find();
        $comment['images'] = $comment['images']?explode(',',$comment['images']):'';
        $this->success('信息返回成功',$comment);
    }


}
